using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using FullMotion.LiveForSpeed.InSim;

namespace OvalRaceControl
{
    class buttongroup
    {
        private int _racerlistX = 2;
        private int _racerlistY = 5;
        private int _racerlistwidth = 40;
        private byte _racerlistheight = 4;

        LfsButton[] racerlistList = new LfsButton[47];

        LfsButton[] racemessagesList = new LfsButton[6];

        LfsButton[] buttonlist = new LfsButton[250];

        private Hashtable _racerDatabase;
        private InSimHandler _handler;

        private int _buttonidCounter = 0;
        private int _RaceMessagesCounter = 0;

        //private List<LfsButton> buttons = new List<LfsButton>(250);
        public buttongroup()
        {

        }
        public buttongroup(InSimHandler handler, Hashtable ht)
        {
            _handler = handler;
            _racerDatabase = ht;

            //CreateRacerList();
            //CreateRaceMessagesList();

            //DisplayRacerList();
            //DisplayRaceMessagesList();
            
        }
        public void Create()
        {
            int counter = 0;
            for (int i = _buttonidmin; i <= _buttonidmax; i++)
            {
                _buttons[i].Left = _x;
                _buttons[i].Height = (byte)_buttonheight;
                _buttons[i].Top = (byte)(_y + (counter * _buttonheight));
                _buttons[i].Width = (byte)_buttonwidth;               
                _buttons[i].Clickable = true;
                _buttons[i].Color = FullMotion.LiveForSpeed.InSim.Enums.ButtonColor.Dark;
                _buttons[i].TextAlignment = FullMotion.LiveForSpeed.InSim.Enums.ButtonTextAlignment.Left;
                _buttons[i].TextColor = FullMotion.LiveForSpeed.InSim.Enums.ButtonTextColor.TitleColor;
                _buttons[i].Text = "dummy";
                counter++;
            }
        }

        public void Specialize()
        {
            if (_name.Equals("RCM Menu"))
            {
                _buttons[_buttonidmin].Text = "New RCM";
                _buttons[_buttonidmin].DialogTextLimit = 95;
                _buttons[_buttonidmin].DialogCaption = "Enter your RCM message";
                _buttons[_buttonidmin].DialogTextLimit = 95;
                _buttons[_buttonidmin].InitializeDialogWithButtonText = false;


                _buttons[_buttonidmin + 1].Text = "Display RCM to All";
                _buttons[_buttonidmin + 2].Text = "Clear RCM to All";
                _buttons[_buttonidmin + 3].Text = "Display RCM to Username";
                _buttons[_buttonidmin + 3].DialogCaption = "Enter your username";
                _buttons[_buttonidmin + 3].DialogTextLimit = 95;
                _buttons[_buttonidmin + 3].InitializeDialogWithButtonText = false;


                _buttons[_buttonidmin + 4].Text = "Clear RCM to username";
                _buttons[_buttonidmin + 4].DialogCaption = "Enter your username";
                _buttons[_buttonidmin + 4].DialogTextLimit = 95;
                _buttons[_buttonidmin + 4].InitializeDialogWithButtonText = false;

                _buttons[_buttonidmax + 5].Text = "Save RCM [todo]";


            }


            if (_name.Equals("Close"))
            {
                _buttons[_buttonidmin].Text = "Close";
            }
        }

        public void Display()
        {
            for (int i = _buttonidmin; i <= _buttonidmax; i++)
            {
                _handler.ShowButton(_buttons[i], false);
            }
        }

        public void Hide()
        {
            for (int i = _buttonidmin; i <= _buttonidmax; i++)
            {
                _handler.DeleteButton(_buttons[i], false);
            }
        }


        public void ClearRacerMessagesList()
        {
            Console.WriteLine(_buttonidCounter);
            for (int i = _buttonidCounter; i < _buttonidCounter + 5; i++)
            {
                if (buttonlist[i] != null)
                _handler.DeleteButton(buttonlist[i], false);
                
            }
            Console.WriteLine(_buttonidCounter);
        }

        private void CreateRaceMessagesList()        
        {
            _RaceMessagesCounter = _buttonidCounter;
            Console.WriteLine("_RaceMessagesCounter" + _RaceMessagesCounter);

            for (int i = _buttonidCounter; i < (_buttonidCounter + 6); i++)
            {
                buttonlist[i] = new LfsButton((byte)i);
                buttonlist[i].Left = (byte)(_racerlistX + _racerlistwidth + 5);
                buttonlist[i].Top = (byte)(_racerlistY + ((_racerlistheight * i) * 2));
                buttonlist[i].Width = (byte)_racerlistwidth;
                buttonlist[i].Height = (byte)(_racerlistheight * 1.5);
                buttonlist[i].Clickable = true;
                buttonlist[i].Color = FullMotion.LiveForSpeed.InSim.Enums.ButtonColor.Dark;
                buttonlist[i].TextAlignment = FullMotion.LiveForSpeed.InSim.Enums.ButtonTextAlignment.Left;
                buttonlist[i].TextColor = FullMotion.LiveForSpeed.InSim.Enums.ButtonTextColor.TitleColor;
            }

            buttonlist[_RaceMessagesCounter].Text = "New RCM";
            buttonlist[_RaceMessagesCounter].DialogCaption = "Enter your RCM message";
            buttonlist[_RaceMessagesCounter].DialogTextLimit = 95;
            buttonlist[_RaceMessagesCounter].InitializeDialogWithButtonText = false;
            
            buttonlist[_RaceMessagesCounter + 1].Text = "Display RCM to All";
            buttonlist[_RaceMessagesCounter + 2].Text = "Clear RCM to All";
            buttonlist[_RaceMessagesCounter + 3].Text = "Display RCM to Username";
            
            buttonlist[_RaceMessagesCounter + 4].Text = "Clear RCM to username";
            buttonlist[_RaceMessagesCounter + 5].Text = "Save RCM";


            _buttonidCounter += 6;
        }

        private void CreateRacerList()
        {
            /*for (int i = _buttonidCounter; i < 47; i++)
            {
                buttonlist[i] = new LfsButton((byte)i);
                buttonlist[i].Left = (byte)_racerlistX;
                buttonlist[i].Top = (byte)(_racerlistY + (_racerlistheight * i));
                buttonlist[i].Height = _racerlistheight;
                buttonlist[i].Text = "Dummy Text " + i;
                buttonlist[i].Width = (byte)this._racerlistwidth;
                buttonlist[i].TextAlignment = FullMotion.LiveForSpeed.InSim.Enums.ButtonTextAlignment.Left;
                buttonlist[i].Clickable = true;
                buttonlist[i].TextColor = FullMotion.LiveForSpeed.InSim.Enums.ButtonTextColor.TextString;

                buttonlist[i].Color = FullMotion.LiveForSpeed.InSim.Enums.ButtonColor.Dark;

                
            }*/

            _buttonidCounter += 47;
        }

        public void DisplayRaceMessagesList()
        {

            for (int i = _RaceMessagesCounter; i < _RaceMessagesCounter + 6; i++)
            {
                _handler.ShowButton(this.buttonlist[i], false);
            }

        }
        public void DisplayRacerList()
        {
            // need to check somehow how many players there are, and strech the buttons nicely across the height to the screen.
            // otherwise you have small buttons with only 10 people on the server, for example.
            Console.WriteLine("_handler.LastLFSState.NumberOfPlayers " + _handler.LastLFSState.NumberOfPlayers);
            int counter = 0;
            foreach (DictionaryEntry entry in _racerDatabase)
            {
                lfsracer racer = (lfsracer)entry.Value;
                racerlistList[counter].Text = racer.username;
                _handler.ShowButton(racerlistList[counter],false);
                counter++;
            }

            for (int i = counter; i < 47; i++)
            {
                _handler.DeleteButton(racerlistList[i], false);
            }


                //racerlistList[i].Text = "";
//                _handler.ShowButton(racerlistList[i], false); 

        }



        public string buttontext(byte buttonid)
        {
            for (int i = 0; i < racerlistList.Length; i++)
            {
                if (racerlistList[i].ButtonId == buttonid)
                    return racerlistList[i].Text;                
            }
            return "";

        }










        private int _buttonidmin;
        public int ButtonIDMin
        {
            get { return _buttonidmin; }
            set { _buttonidmin = value; }
        }

        private int _buttonidmax;
        public int ButtonIDMax
        {
            get { return _buttonidmax; }
            set { _buttonidmax = value; }
        }

        private int _buttonheight;
        public int ButtonHeight
        {
            get { return _buttonheight; }
            set { _buttonheight = value; }
        }

        private int _buttonwidth;

        public int ButtonWidth
        {
            get { return _buttonwidth; }
            set { _buttonwidth = value; }
        }
	

        private string _name;

        public string GroupName
        {
            get { return _name; }
            set { _name = value; }
        }


        private byte _x;

        public byte X
        {
            get { return _x; }
            set { _x = value; }
        }

        private byte _y;

        public byte Y
        {
            get { return _y; }
            set { _y = value; }
        }


        private List<LfsButton> _buttons;

        public List<LfsButton> ButtonList
        {
            get { return _buttons; }
            set { _buttons = value; }
        }
                













    }
}
